package cn.shyouchi.wuguanjia.android.ui.event.proprietor;

import android.content.Context;
import android.support.annotation.NonNull;

import java.util.List;

import cn.shyouchi.wuguanjia.android.api.event.EventApi;
import cn.shyouchi.wuguanjia.android.model.BaseRequestResult;
import cn.shyouchi.wuguanjia.android.model.OwnerEntity;
import cn.shyouchi.wuguanjia.android.subscribers.ProgressSubscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by Eenie on 2016/11/15 at 9:29
 * Email: 472279981@qq.com
 * Des:
 */

public class EventProprietorPresenter implements ProprietorInfoContract.Presenter {

    EventApi mEventApi;
    ProprietorInfoContract.View mView;
    Context mContext;

    public EventProprietorPresenter(Context context, ProprietorInfoContract.View view) {
        this.mContext = context;
        mEventApi = new EventApi(context);
        attachView(view);
    }


    @Override
    public void getOwners(String projectId) {
        mEventApi.getOwnerList(projectId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .map(new Func1<OwnerEntity, List<OwnerEntity.ProprietorBean>>() {
                    @Override
                    public List<OwnerEntity.ProprietorBean> call(OwnerEntity entity) {
                        return entity.getResultMessage();
                    }
                })
                .subscribe(new ProgressSubscriber<List<OwnerEntity.ProprietorBean>>(mContext) {
                    @Override
                    public void onNext(List<OwnerEntity.ProprietorBean> proprietors) {
                        mView.onOwnersRefresh(proprietors);
                    }
                });
    }

    @Override
    public void delOwner(final String projectId, String id) {
        mEventApi.delOwner(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new ProgressSubscriber<BaseRequestResult>(mContext) {
                    @Override
                    public void onNext(BaseRequestResult baseRequestResult) {
                        getOwners(projectId);
                    }
                });
    }

    @Override
    public void attachView(@NonNull ProprietorInfoContract.View view) {
        this.mView = view;
    }

    @Override
    public void detachView() {
        this.mView = null;
    }
}
